<template>
    <div>
        <el-row>
            <el-button type="primary" @click="addgsForm"><i class="el-icon-plus"></i>新增分类</el-button>
        </el-row>
        <el-dialog :title="title" :visible.sync="dialogAddgsVisible"
                   @close="closeDialogAddgsVisible">
            <el-form ref="addForm" :model="addForm">
                <el-form-item label="新增分类">
                    <el-input v-model="addForm.name" placeholder="请输入分类名称"></el-input>
                </el-form-item>
                <el-form-item label="排序值">
                    <el-input v-model="addForm.orderNum" placeholder="请输入排序值"></el-input>
                </el-form-item>
                <el-form-item label="目录级别">
                    <el-input v-model="addForm.type" placeholder="请输入目录级别"></el-input>
                </el-form-item>
                <el-form-item label="上级目录ID">
                    <el-input v-model="addForm.parentId" placeholder="请输入上级目录ID"></el-input>
                </el-form-item>

            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogAddgsVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAddForm()">确 定</el-button>
            </div>
        </el-dialog>

        <el-table :data="tableData" border style="width: 100%">
            <el-table-column fixed prop="id" label="分类ID" width="200"></el-table-column>
            <el-table-column fixed prop="name" label="分类名称" width="300"></el-table-column>
            <el-table-column fixed prop="orderNum" label="排序值" width="200"></el-table-column>
            <el-table-column fixed prop="createTime" label="添加时间" width="300"></el-table-column>
            <el-table-column fixed="right" label="操作" width="150">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="updateForm(scope.row)">编辑</el-button>
                    <el-button @click="deleteProductById(scope.row.id)" type="text" size="small">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="m-pagination">
            <el-pagination
                    :hide-on-single-page="value"
                    :total="pageTotal"
                    layout="prev, pager, next"
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
            ></el-pagination>
        </div>
    </div>
</template>

<script>
    import {adminCategory, adminAddCategory, adminDeleteCategory} from "../../../utils/apis";

    export default {
        name: "adminCategory",
        data() {
            return {
                pageNum: 1,
                value: false,
                pageSize: 10,
                pageTotal: 0,
                tableData: [],
                title: '',
                dialogAddgsVisible: false,
                addForm: {
                    id: '',
                    name: '',
                    type: '',
                    parentId: '',
                    orderNum: ''
                }
            }
        },
        mounted() {
            this.categoryList()
        },
        methods: {
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.pageNum = val
                this.categoryList()
            },
            deleteProductById(id) {
                console.log(id)
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    adminDeleteCategory({id: id}).then(res => {
                        console.log(res)
                        if (res.status === 10000) {
                            this.dialogAddgsVisible = false;
                            this.$message({
                                title: "success",
                                message: "success",
                                type: "success"
                            });
                            this.categoryList()
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            categoryList() {
                adminCategory({
                    "pageNum": this.pageNum,
                    "pageSize": this.pageSize
                }).then(res => {
                    console.log(res)
                    this.tableData = res.data.list
                    this.pageTotal = res.data.total
                })
            },
            addgsForm() {
                this.dialogAddgsVisible = true;
                //初始化
                this.addForm = {
                    id: '',
                    name: '',
                    type: '',
                    parentId: '',
                    orderNum: ''
                }
                this.title = "新增类目"
            },
            closeDialogAddgsVisible() {

                this.$refs.addForm.resetFields();//element封装的方法,清空模态框的值
                this.title = "" //初始化title的值
                this.addForm = {
                    id: '',
                    name: '',
                    type: '',
                    parentId: '',
                    orderNum: ''
                }
                this.dialogVisible = false
            },
            saveAddForm() {
                this.addForm.status = 1
                adminAddCategory({...this.addForm}).then(res => {
                    console.log(res)
                    if (res.status === 10000) {
                        this.dialogAddgsVisible = false;
                        this.$message({
                            title: "success",
                            message: "success",
                            type: "success"
                        });
                        this.categoryList()
                    }

                })
            },
            updateForm(category) {
                console.log(category)
                this.addForm.name = category.name
                this.addForm.id = category.id
                this.addForm.type = category.type
                this.addForm.parentId = category.parentId
                this.addForm.orderNum = category.orderNum
                this.dialogAddgsVisible = true;
                this.title = "更新类目"
            },

        }
    }
</script>

<style scoped>
    .m-pagination {
        width: 100%;
        text-align: right;
    }
</style>
